/*
    This file is part of Emu.

    Emu is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Emu is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with Emu.  If not, see <http://www.gnu.org/licenses/>. */
#include "esplitterhandle.h"

/* This code was based off of the idea found here:
   http://ariya.blogspot.com/2005/10/improving-qsplitter-with-quantized.html
   (Last fetched Nov 15 2013)
   Changes made:
   * The bottom window grows by steps, not the top.
   * Take the textedit's margins into account (2px for me).
   * Take the extra space needed for the first line into account (This may just
     be the font that I selected though.)
   * Implement a majority of the code in a helper function.
   * Since the bottom window is growing by steps, the splitter's handle is also
     added in at the end. This is because the handle draws down from where it
     starts. This isn't needed if the top window is growing by steps. */

ESplitterHandle::ESplitterHandle(ESplitter *splitter, int lineMargin) :
    QSplitterHandle(Qt::Vertical, splitter), _splitter(splitter),
    _lineSpacing(0), _margin(lineMargin), _extra(0)
{
}

void ESplitterHandle::updateFontInfo(int lineSpacing, int extra)
{
    _lineSpacing = lineSpacing;
    _extra = extra;
}

void ESplitterHandle::mouseMoveEvent(QMouseEvent *event)
{
    if((event->buttons() & Qt::LeftButton) == false)
        return;

    int pos = determineHandlePos(event);
    if (pos == -1)
        return;

    _splitter->setRubberBand(closestLegalPosition(pos));
}

void ESplitterHandle::mouseReleaseEvent(QMouseEvent *event)
{
    _splitter->setRubberBand(-1);

    int pos = determineHandlePos(event);
    if (pos == -1)
        return;

    moveSplitter(pos);
}

int ESplitterHandle::determineHandlePos(QMouseEvent *event)
{
    /* This will be a position relative to the top of the widget (the height of
       the top window. */
    int pos = parentWidget()->mapFromGlobal(event->globalPos()).y();
    /* Since there are only two windows, do this to determine the height for the
       bottom window. This is because the bottom window is growing by steps, not
       the top one. */
    int parentHeight = _splitter->size().height();
    int posFromBottom = parentHeight - pos;
    /* This enforces a minimum of 1 line of space. */
    if (posFromBottom < _lineSpacing)
        posFromBottom = _lineSpacing;

    posFromBottom = (int)(posFromBottom / _lineSpacing) * _lineSpacing;

    /* Add in the text margins, the extra space for the first line, and the
       splitter handle. The splitter's handle width is necessary because the
       rubber band/splitter gets drawn down from the spot it's given. */
    posFromBottom += _margin + _extra + _splitter->handleWidth();
    int closestLegal = closestLegalPosition(parentHeight - posFromBottom);
    if (closestLegal != parentHeight - posFromBottom)
        return -1;

    return parentHeight - posFromBottom;
}
